﻿using AvalSqlIndexManager.ViewModels;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AvalSqlIndexManager.Models
{
    public class Node: ViewModelBase
    {
        public ObservableCollection<Node>? SubNodes { get; }

        private string _title;
        public string Title
        {
            get
            {
                if (TableIndex != null && TableIndex.FieldsName != null)
                    return TableIndex.FieldsName;
                else
                    return _title;
            }
            set { this.RaiseAndSetIfChanged(ref _title, value); }
        }
        /// <summary>
        /// 如果为null，即为table节点，不允许修改
        /// </summary>
        public TableIndex? TableIndex { get; set; }
        public Node()
        {
            Title = string.Empty;
        }
        public Node(string title)
        {
            Title = title;
        }

        public Node(string title, ObservableCollection<Node> subNodes)
        {
            Title = title;
            SubNodes = subNodes;
        }

        public void AddNode(TableIndex tableIndex) {
            if (SubNodes == null)
                return;
            if (string.IsNullOrEmpty(tableIndex.FieldsName))
                return;
            Node subNode = new Node(tableIndex.FieldsName);
            subNode.TableIndex = tableIndex;
            SubNodes.Add(subNode);
        }
    }
}
